/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package mx.edu.itschapala.sistemas.septimo.vista;

import java.awt.event.KeyEvent;
import mx.edu.itschapala.sistemas.septimo.control.*;
import mx.edu.itschapala.sistemas.septimo.modelo.Producto;
import mx.edu.itschapala.sistemas.septimo.vista.enumeracion.TipoVentana;

/**
 *
 * @author Estudiante
 */
public class VisProducto extends javax.swing.JFrame {

    /**
     * Creates new form Producto
     */
    private ProductoLnLocal productoControl;
    private TipoVentana tipoVentana;
    private Producto producto;
    
    
    public VisProducto(Producto producto,ProductoLnLocal productoControl, TipoVentana tipoVentana) {
        this.productoControl = productoControl;
        this.tipoVentana = tipoVentana;
        initComponents();
        switch (tipoVentana){
            case VENTANA_REGISTRO:
              jLabel1.setText("Registrar Producto");
                break;
            case VENTANA_MODIFICAR:
                jLabel1.setText("MODIFICA PRODUCTO");
                break;
            case VENTANA_BUSQUEDA:
                jLabel1.setText("Buscar por ID");
                jButton1.setText("Buscar");
                jTextField2.setEditable(false);
                jTextField3.setEditable(false);
                jTextField4.setEditable(false);
                jTextField5.setEditable(false);
                break;
            case VENTANA_ELIMINAR:
                jLabel1.setText("Eliminar");
                jButton2.setText("Eliminar");
                break;
        }
    }
    

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jTextField3 = new javax.swing.JTextField();
        jTextField4 = new javax.swing.JTextField();
        jTextField5 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();
        jLabel8 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel1.setText("PRODUCTO");
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(162, 20, -1, -1));

        jTextField1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextField1KeyPressed(evt);
            }
        });
        getContentPane().add(jTextField1, new org.netbeans.lib.awtextra.AbsoluteConstraints(104, 52, 140, 25));

        jTextField2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField2ActionPerformed(evt);
            }
        });
        getContentPane().add(jTextField2, new org.netbeans.lib.awtextra.AbsoluteConstraints(104, 88, 140, 25));
        getContentPane().add(jTextField3, new org.netbeans.lib.awtextra.AbsoluteConstraints(104, 124, 140, 25));
        getContentPane().add(jTextField4, new org.netbeans.lib.awtextra.AbsoluteConstraints(104, 160, 140, 25));
        getContentPane().add(jTextField5, new org.netbeans.lib.awtextra.AbsoluteConstraints(104, 196, 140, 25));

        jButton1.setText("ACEPTAR");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(104, 244, -1, -1));

        jLabel2.setText("NOMBRE");
        getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 88, 90, 25));

        jLabel3.setText("PRECIO");
        getContentPane().add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 124, 90, 25));

        jLabel4.setText("CODIGO");
        getContentPane().add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 52, 90, 25));

        jLabel5.setText("DEPARTAMENTO");
        getContentPane().add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 160, 90, 25));

        jLabel6.setText("DESCRIPCION");
        getContentPane().add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 196, 90, 25));

        jButton2.setText("CANCELAR");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(201, 244, -1, -1));

        jLabel8.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/maverick-6.jpg"))); // NOI18N
        getContentPane().add(jLabel8, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 400, 300));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jTextField2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField2ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
      switch (tipoVentana) {
            case VENTANA_REGISTRO:
                Producto p = new Producto();
                p.setCodigo(Integer.parseInt(jTextField1.getText()));
                p.setNombre(jTextField2.getText());
                p.setPrecio(Float.parseFloat(jTextField3.getText()));
                p.setDepartamento(jTextField4.getText());
                p.setDescripcion(jTextField5.getText());
                productoControl.registrar(p);
                jTextField1.setText("");
                jTextField2.setText("");
                jTextField3.setText("");
                jTextField4.setText("");
                jTextField5.setText("");
                break;
            case VENTANA_BUSQUEDA:
                this.dispose();
                break;
            case VENTANA_MODIFICAR:
                p = new Producto();
                p.setCodigo(Integer.parseInt(jTextField1.getText()));
                p.setNombre(jTextField2.getText());
                p.setPrecio(Float.parseFloat(jTextField3.getText()));
                p.setDepartamento(jTextField4.getText());
                p.setDescripcion(jTextField5.getText());
                productoControl.modificar(p);
                jTextField1.setText("");
                jTextField2.setText("");
                jTextField3.setText("");
                jTextField4.setText("");
                jTextField5.setText("");
                jTextField2.setEditable(false);
                jTextField3.setEditable(false);
                jTextField4.setEditable(false);
                jTextField5.setEditable(false);
                break;
            case VENTANA_ELIMINAR:
                p = new Producto();
                p.setCodigo(Integer.parseInt(jTextField1.getText()));
                productoControl.eliminar(p);
                
        }


    
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
         this.dispose();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jTextField1KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField1KeyPressed
          if(tipoVentana!=TipoVentana.VENTANA_REGISTRO){
            if(evt.getKeyCode()==KeyEvent.VK_ENTER){
                Producto p=productoControl.buscarPorCodigo(Integer.parseInt(jTextField1.getText()));
                if(p!=null){
                    jTextField2.setText(p.getNombre());
                    jTextField3.setText(p.getPrecio()+"");
                    jTextField4.setText(p.getDepartamento());
                    jTextField5.setText(p.getDescripcion());
                    if(tipoVentana==TipoVentana.VENTANA_MODIFICAR){
                        jTextField2.setEditable(true);
                        jTextField3.setEditable(true);
                        jTextField4.setEditable(true);
                        jTextField5.setEditable(true);
                    }
                    if(tipoVentana==TipoVentana.VENTANA_ELIMINAR){
                        productoControl.eliminar(p);
                    }
                }
            }
        }
    }//GEN-LAST:event_jTextField1KeyPressed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    private javax.swing.JTextField jTextField5;
    // End of variables declaration//GEN-END:variables
}
